<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\AssessTemplateFormat;
use App\Format\AssessTemplateRuleFormat;
use App\Http\Requests\CreateAssessTemplateRequest;
use App\Services\AssessTemplateService;
use Illuminate\Http\Request;

class AssessTemplateController extends Controller
{
    protected $assessTemplateService;

    public function __construct(AssessTemplateService $assessTemplateService)
    {
        $this->assessTemplateService = $assessTemplateService;
    }

    public function create(CreateAssessTemplateRequest $createAssessTemplateRequest)
    {
        $assessTemplateFormat = new AssessTemplateFormat($createAssessTemplateRequest->input('assess_template'));
        $assessTemplateRules = $createAssessTemplateRequest->input('assess_template_rules');
        $rules = [];
        foreach ($assessTemplateRules as $assessTemplateRule) {
            array_push($rules, new AssessTemplateRuleFormat($assessTemplateRule));
        }
        $this->assessTemplateService->create($assessTemplateFormat, $rules);
        return renderSuccessJson();
    }

    public function update(Request $request)
    {
        $assessTemplateFormat = new AssessTemplateFormat($request->input('assess_template'));
        $assessTemplateRules = $request->input('assess_template_rules');
        $rules = [];
        if (! empty($assessTemplateRules)) {
            foreach ($assessTemplateRules as $assessTemplateRule) {
                array_push($rules, new AssessTemplateRuleFormat($assessTemplateRule));
            }
        }
        $this->assessTemplateService->update($assessTemplateFormat, $rules);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $this->assessTemplateService->delete($id);
        return renderSuccessJson();
    }

    public function getLists(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('type')) {
            $search['type'] = $request->input('type');
        }
        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }
        $res = $this->assessTemplateService->getList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function getTempToAssess(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('type')) {
            $search['type'] = $request->input('type');
        } else {
            throw new ApiException('type 缺失');
        }
        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }
        $res = $this->assessTemplateService->getTempToAssess($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        $detail = $this->assessTemplateService->detail($id);
        return renderSuccessJson($detail);
    }

    public function status(Request $request)
    {
        $id = $request->input('id');
        $status = $request->input('status');
        if (! $id || ! $status) {
            throw new ApiException('参数异常', 1);
        }
        $this->assessTemplateService->status($id, $status);
        return renderSuccessJson();
    }
}
